package com.qdu.servlet;

import com.qdu.util.DatabaseUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 处理根据学号删除单个学生请求的Servlet
 *
 * @author Anna
 */
@WebServlet("/a_doss")
public class Admin_DeleteServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1. 获取请求参数
        //获取要删除的学生的学号
        String rollno = req.getParameter("sid");
        String option = req.getParameter("option");
        //2. 执行业务逻辑
        //根据学号从数据库删除学生
//        String driver_class = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//        String url = "jdbc:sqlserver://localhost:1433;databaseName=Crp";
//        String username = "sa";
//        String password = "sa";

        Connection con = null;
        PreparedStatement ps = null;
        int rows = 0;

        try {
//            Class.forName(driver_class);
//            con = DriverManager.getConnection(url, username, password);
con = DatabaseUtil.getConnection();
            if (option.equals("User")) {
                ps = con.prepareStatement("DELETE FROM [dbo].[User] WHERE id=?");
                 ps.setString(1, rollno);
            } else {
                ps = con.prepareStatement("DELETE FROM [dbo].[Admin] WHERE id=?");
                 ps.setString(1, rollno);
            }

           
            rows = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {

                if (null != ps) {
                    ps.close();
                }
                if (null != con) {
                    con.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }

        //3. 生成响应内容，因为这里删除学生使用ajax发送的请求，不打算给任何响应数据，所以就没打印响应内容
    }
}
